//  Abstract:
//    See wrapper-xslt.h
//
//  Reference:
//    See wrapper-xslt.h
//
//  Design Notes:
//    See wrapper-xslt.h
//
//  Disclaimer:
//    Copyright (C) 2008, Janice Law <janicel700@gmail.com>.
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//  Change Log:
//
#include <libxml++/libxml++.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include "wrapper-xslt.h"
#include <iostream>

using namespace std;
using namespace boost;
using Glib::ustring;

namespace Epub
{
Xslt::Xslt()
{
    xsltInit();
}

Xslt::~Xslt()
{
    xsltCleanupGlobals();
}

shared_ptr<xmlpp::Document> Xslt::Transform(shared_ptr<xmlpp::Document> source)
{
    ustring fileName = PACKAGE_DATA_DIR;
    fileName = fileName + "/gopsed.xslt"; 
    xsltStylesheetPtr
            xslt_document = xsltParseStylesheetFile((xmlChar*)fileName.c_str());
    xmlDocPtr result_document = xsltApplyStylesheet(xslt_document,
            source->cobj(), NULL);
    xsltFreeStylesheet(xslt_document);
    xmlChar* xml_string;
    int size_byte;
    xmlDocDumpFormatMemory(result_document, &xml_string, &size_byte, 0);

    scoped_ptr<xmlpp::DomParser> parser(new xmlpp::DomParser);
    parser->set_substitute_entities();
    parser->parse_memory((char *)xml_string);
    shared_ptr<xmlpp::Document> result(new xmlpp::Document);
    result->create_root_node_by_import(parser->get_document()->get_root_node());
    xmlFree(xml_string);
    return result;
}
} // Epub
